package utils;

import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import org.kordamp.bootstrapfx.BootstrapFX;

import java.io.*;
import java.net.URL;

public class ControllerUtils {

    /*
     * @Author Dongle
     * @Description 关闭界面
     * @Date 20:14 2022/12/27
     * @Param
     * @param pane
     * @return
     **/
    public static void closePane(Pane pane) {
        getStage(pane).close();

    }

    /*
     * @Author Dongle
     * @Description 获取 Stage
     * @Date 20:14 2022/12/27
     * @Param
     * @param pane
     * @return
     **/
    public static Stage getStage(Pane pane) {
        return (Stage) pane.getScene().getWindow();
    }

    public static Stage createStage(Parent root) {
        Scene scene = new Scene(root);
        scene.getStylesheets().add(BootstrapFX.bootstrapFXStylesheet());
        Stage stage = new Stage();
        stage.setScene(scene);
        return stage;
    }

    public static void writeSession(String session) {
        String path = "./session.txt";
        System.out.println("登陆 session 存入本地：" + path);
        try {
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(path));
            bufferedWriter.write(session);
            bufferedWriter.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static String getSession(){
        String path = "./session.txt";
        String line;
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(path));
            line = bufferedReader.readLine();
            bufferedReader.close();
        } catch (IOException e) {
            System.out.println("session 文件不存在");
            return null;
        }
        return line;
    }

    
    public static FXMLLoader getLoaderFromFxml(URL path) {
        return new FXMLLoader(path);
    }

}
